﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

public partial class XSSJ_XSSJAdd : System.Web.UI.Page
{
  public string mode = "", cbsjid = "", khid = "", bccbsjid = "";
  protected void Page_Load(object sender, EventArgs e)
  {
    InitParam();
    if (!CommonUse.IsInt(khid) || !CommonUse.IsInt(cbsjid) || !CommonUse.IsInt(bccbsjid))
      return;
    if (!Page.IsPostBack)
    {
      if (!ProjectPrivate.GetQX("48"))
      {
        Server.Transfer("~\\blank.aspx");
      }
      //if (CWSD.GetSDZT("是"))
      //{
      //  Server.Transfer("~\\sd.aspx");
      //}
      DataSet ds = CBR.GetDS_W();
      CommonUse.BindDDL(ds, "cbrid", "cbr", "0", ddlCBR);
      InitKHXX();
      InitSC();
      InitYQXZ();
      InitData();
      if (mode == "sc")
        ASPxHyperLink1.NavigateUrl = "XSSJKHList.aspx";
      else if (mode == "bc")
        ASPxHyperLink1.NavigateUrl = "XSSJList.aspx";
    }
    //InitPanel_TZQ();
    InitPanel();
  }

  protected void InitParam()
  {
    mode = CommonUse.GetID("mode");
    cbsjid = CommonUse.GetID("cbsjid");
    khid = CommonUse.GetID("khid");
    bccbsjid = CommonUse.GetID("bccbsjid");
  }

  protected void InitKHXX()
  {
    lbKHID.Text = khid;
    SqlDataReader dr = KHXX.GetXX(lbKHID.Text);
    if (dr.Read())
    {
      lbKHMC.Text = dr["khmc"].ToString();
      lbBS.Text = dr["kh"].ToString() + "&nbsp;";
    }
    dr.Close();
  }

  protected void InitSC()
  {
    DataSet ds_cbsj = CBSJ.Get_V_CBSJ(cbsjid);
    if (ds_cbsj.Tables[0].Rows.Count > 0)
    {
      lbSCBS.Text = ds_cbsj.Tables[0].Rows[0]["bybs"].ToString();
      lbSCCBR.Text = ds_cbsj.Tables[0].Rows[0]["cbr"].ToString();
      lbSCCBSJ.Text = ds_cbsj.Tables[0].Rows[0]["rq"].ToString();
      ddlCBR.SelectedValue = ds_cbsj.Tables[0].Rows[0]["cbrid"].ToString();
    }
  }

  protected void InitData()
  {
    dateJG.Text = CBSJ.Get_TZSJ();
    if (mode == "bc")
    {
      DataSet ds = CBSJ.Get_V_CBSJ(bccbsjid);
      if (ds.Tables[0].Rows.Count > 0)
      {
        dateJG.Text = ds.Tables[0].Rows[0]["rq"].ToString();
        //txtName.Text = dr["bybs"].ToString();
        //hlName.Value = txtName.Text;
        //hlJG.Value = dateJG.Text;
        //sKHID = dr["khid"].ToString();
        //ddlCBR.SelectedValue = dr["cbrid"].ToString();
        //hlCBR.Value = ddlCBR.SelectedValue;
        ddlYQXZ.SelectedValue = ds.Tables[0].Rows[0]["yqxzid"].ToString();
        //ddlYQXZ.Enabled = false;
        //dateJG.Enabled = false;
      }
      //initJG();
      initFY();
      //InitPanel();
    }
  }

  protected void InitPanel()
  {
    DataSet ds = null;
    if (mode == "sc")
      ds = KHYQXZ.GetDS(khid, cbsjid);
    else
      ds = JGTZ_BQQL.Get_BQQL_YQXZ(bccbsjid);

    Panel1.Controls.Clear();
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      Panel1.Controls.Add(new LiteralControl("<tr>"));

      Panel1.Controls.Add(new LiteralControl("<td  width=\"25%\" height=20 align=\"right\" bgcolor=\"#E7E7E7\"><font size=3 color=red>"));
      Label lb = new Label();
      lb.ID = "l" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      lb.Text = ds.Tables[0].Rows[i]["yqxz"].ToString();
      Panel1.Controls.Add(lb);
      Panel1.Controls.Add(new LiteralControl("</font></td>"));

      Panel1.Controls.Add(new LiteralControl("<td  width=75% bgcolor=#FFFFFF>"));
      TextBox textBox = new TextBox();
      string id = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      textBox.ID = id;

      textBox.Text = ds.Tables[0].Rows[i]["bqql"].ToString();
      Panel1.Controls.Add(textBox);

      if ((ViewState[id] != null) && ((bool)ViewState[id]))
      { }
      else
      {
        ViewState[id] = true;
      }

      RequiredFieldValidator rfv = new RequiredFieldValidator();
      rfv.ID = "r" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rfv.ErrorMessage = "*";
      rfv.ControlToValidate = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      Panel1.Controls.Add(rfv);

      RangeValidator rv = new RangeValidator();
      rv.ID = "rv" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rv.ErrorMessage = "请输入数字";
      rv.ControlToValidate = "t" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
      rv.MaximumValue = "100000000";
      rv.MinimumValue = "0";
      rv.Type = ValidationDataType.Integer;
      Panel1.Controls.Add(rv);

      Panel1.Controls.Add(new LiteralControl("</td>"));
      Panel1.Controls.Add(new LiteralControl("</tr>"));
    }
  }

  protected void InitPanel_TZQ()
  {
    //DataSet ds = null;
    //ds = BQQL.Get_BQQL_YQXZ(cbsjid);

    //pnTZQ.Controls.Clear();
    //for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    //{
    //  pnTZQ.Controls.Add(new LiteralControl("<tr>"));

    //  pnTZQ.Controls.Add(new LiteralControl("<td  width=\"25%\" height=20 align=\"right\" bgcolor=\"#E7E7E7\"><font size=3 color=blue>"));
    //  Label lb = new Label();
    //  lb.ID = "l2" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
    //  lb.Text = ds.Tables[0].Rows[i]["yqxz"].ToString();
    //  pnTZQ.Controls.Add(lb);
    //  pnTZQ.Controls.Add(new LiteralControl("</font></td>"));

    //  pnTZQ.Controls.Add(new LiteralControl("<td  width=75% bgcolor=#FFFFFF>"));
    //  Label lbQL = new Label();
    //  string id = "t2" + ds.Tables[0].Rows[i]["yqxzid"].ToString();
    //  lbQL.ID = id;
    //  lbQL.Text = ds.Tables[0].Rows[i]["bqql"].ToString();
    //  pnTZQ.Controls.Add(lbQL);

    //  if ((ViewState[id] != null) && ((bool)ViewState[id]))
    //  { }
    //  else
    //  {
    //    ViewState[id] = true;
    //  }

    //  pnTZQ.Controls.Add(new LiteralControl("</td>"));
    //  pnTZQ.Controls.Add(new LiteralControl("</tr>"));
    //}
  }

  protected void btnConfirm_Click(object sender, EventArgs e)
  {
    if (string.IsNullOrEmpty(dateJG.Text))
    {
      lbMessage.Text = "抄表时间不能为空";
      return;
    }

    if (ddlYQXZ.SelectedValue == "0")
    {
      lbMessage.Text = "请选择用气性质";
      return;
    }
    int scql = Convert.ToInt32(lbBCQL.Text);
    //DataSet dsSC = BQQL.Get_BQQL_YQXZ(cbsjid);
    //for (int i = 0; i < dsSC.Tables[0].Rows.Count; i++)
    //{
    //  Label txt = (Label)pnTZQ.FindControl("t2" + dsSC.Tables[0].Rows[i]["yqxzid"].ToString());
    //  scql += Convert.ToInt32(txt.Text);
    //}

    DataSet ds = new DataSet();
    ds = KHYQXZ.GetDS(khid, cbsjid);
    int bcql = 0;
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      TextBox txt = (TextBox)Panel1.FindControl("t" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
      bcql += Convert.ToInt32(txt.Text);
    }
    if (scql != bcql)
    {
      lbMessage.Text = "本次拆分气量不等于上次气量";
      return;
    }

    if (mode == "sc")
      Insert(ds);
    else if (mode == "bc")
      Update(ds);
  }

  protected void Save(string sID, DataSet ds)
  {
    #region
    //if (CBSJ.ExistHQID(lbKHID.Text, dateJG.Text))
    //{
    //  lbMessage.Text = "本次日期之后已存在数据，不能修改";
    //  return;
    //}
    //if (CBSJ.ExistWSBSJ(lbKHID.Text, dateJG.Text))
    //{
    //  lbMessage.Text = "存在未上报数据，请先处理";
    //  return;
    //}
    //if (CBSJ.ExistWSHSJ(lbKHID.Text, dateJG.Text))
    //{
    //  lbMessage.Text = "存在已上报未审核数据，请先处理";
    //  return;
    //}
    //if (!CommonUse.IsInt(txtName))
    //{
    //  lbMessage.Text = "本次表数必须为整数";
    //  return;
    //}

    //string sSYBS = CBSJ.GetSCBS(lbKHID.Text, Convert.ToDateTime(dateJG.Text));
    //string sSCCBSJ = CBSJ.GetSCCBSJ(lbKHID.Text, Convert.ToDateTime(dateJG.Text));

    //DataSet dsYQXZ = KHXX.GetYQXZ(lbKHID.Text);
    //if (dsYQXZ.Tables[0].Rows.Count == 0)
    //{
    //  lbMessage.Text = "该用户未选择用气性质，请与管理员联系";
    //  return;
    //}

    //SqlParameter[] cmdParams = null;
    //string sSql = "";
    #endregion
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci = new CommandInfo();
    //ci = CBSJ.Delete(bccbsjid);
    //cis.Add(ci);
    //ci = BQQL.Delete(bccbsjid);
    //cis.Add(ci);
    //ci = JGTZ_CBSJ.Delete(bccbsjid);
    //cis.Add(ci);
    //ci = JGTZ_BQQL.Delete(bccbsjid);
    CBSJ_Model model = new CBSJ_Model();
    model.CBSJID = Convert.ToInt32(sID);
    model.DLM = SysManage.Get_User;
    model.CBRID = Convert.ToInt32(ddlCBR.SelectedValue);
    model.KHID = Convert.ToInt32(lbKHID.Text);
    model.CBSJ = Convert.ToDateTime(dateJG.Text);
    model.BYBS = Convert.ToInt32(lbSCBS.Text);
    model.SJYQL = 0;
    model.CBBZ = txtBZ.Text;
    model.CBSJ_SFSB = "0";
    model.YSQL = 0;
    model.SCBS = Convert.ToInt32(lbSCBS.Text);
    model.SCCBSJ = Convert.ToDateTime(lbSCCBSJ.Text);
    ci = CBSJ.Insert(model);
    cis.Add(ci);
    //BQQL_Model bqql_model = new BQQL_Model();
    //bqql_model.YQXZID = Convert.ToInt32(ddlYQXZ.SelectedValue);
    //bqql_model.CBSJID = Convert.ToInt32(sID);
    //bqql_model.BQQL = 0;
    //bqql_model.BQQL_SFSB = "0";
    //ci = BQQL.Add(bqql_model);
    //cis.Add(ci);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      BQQL_Model bqql_model = new BQQL_Model();
      bqql_model.YQXZID = Convert.ToInt32(ds.Tables[0].Rows[i]["yqxzid"].ToString());
      bqql_model.CBSJID = Convert.ToInt32(sID);
      bqql_model.BQQL = 0;
      bqql_model.BQQL_SFSB = "0";
      ci = BQQL.Add(bqql_model);
      cis.Add(ci);
    }
    JGTZ_CBSJ_Model jgtz_model = new JGTZ_CBSJ_Model();
    jgtz_model.CBSJID = Convert.ToInt32(sID);
    jgtz_model.DLM = SysManage.Get_User;
    jgtz_model.CBRID = Convert.ToInt32(ddlCBR.SelectedValue);
    jgtz_model.KHID = Convert.ToInt32(lbKHID.Text);
    jgtz_model.CBSJ = Convert.ToDateTime(dateJG.Text);
    jgtz_model.BYBS = Convert.ToInt32(lbSCBS.Text);
    jgtz_model.BQSJ = Convert.ToInt32(cbsjid);
    jgtz_model.SJYQL = 0;
    jgtz_model.CBBZ = txtBZ.Text;
    jgtz_model.CBSJ_SFSB = "0";
    jgtz_model.YSQL = 0;
    jgtz_model.SCBS = Convert.ToInt32(lbSCBS.Text);
    jgtz_model.SCCBSJ = Convert.ToDateTime(lbSCCBSJ.Text);
    ci = JGTZ_CBSJ.Insert(jgtz_model);
    cis.Add(ci);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      TextBox txt = (TextBox)Panel1.FindControl("t" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
      //if (txt.Text != "0")
      //{
      JGTZ_BQQL_Model jgtz_bqql_model = new JGTZ_BQQL_Model();
      jgtz_bqql_model.YQXZID = Convert.ToInt32(ds.Tables[0].Rows[i]["yqxzid"].ToString());
      jgtz_bqql_model.CBSJID = Convert.ToInt32(sID);
      jgtz_bqql_model.BQQL = Convert.ToInt32(txt.Text);
      jgtz_bqql_model.BQQL_SFSB = "0";
      ci = JGTZ_BQQL.Add(jgtz_bqql_model);
      cis.Add(ci);
      //}
    }

    #region  本期气量
    //cis.Add(CommonUse.DeleteCI("SJLR_bqql", "cbsjid", sID));

    //if (dsYQXZ.Tables[0].Rows.Count == 1)
    //{
    //DataTable dtJG = JGGL.GetJG(dsYQXZ.Tables[0].Rows[0]["yqxzid"].ToString(), dateJG.Text);
    //if (dtJG.Rows.Count == 0)
    //{
    //  lbMessage.Text = "还未录入价格，请与管理员联系";
    //  return;
    //}

    //arrValue = new ArrayList();
    //arrValue.Add(ddlYQXZ.SelectedValue);
    //arrValue.Add(sID);
    //arrValue.Add("0");
    //arrValue.Add("1");
    ////arrValue.Add((Convert.ToDouble(lbBCJG.Text) - Convert.ToDouble(lbSCJG.Text)).ToString());
    ////arrValue.Add("0");
    ////arrValue.Add(txtName.Text);
    ////arrValue.Add("0");
    ////arrValue.Add(txtName.Text);
    //arrValue.Add(DBNull.Value);
    //arrValue.Add(DBNull.Value);
    //arrValue.Add(DBNull.Value);
    //arrValue.Add(DBNull.Value);
    //arrValue.Add(DBNull.Value);
    //arrValue.Add(DBNull.Value);
    //arrLengh = new ArrayList();
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //arrLengh.Add("20");
    //cis.Add(CommonUse.InsertCI("sjlr_bqql", arrValue, arrLengh));

    //sSql = @"update sjlr_bqql set ysjg=ysfy where yqxzid=@0 and cbsjid=@1";
    //cmdParams = new SqlParameter[2] { new SqlParameter("@0", SqlDbType.VarChar, 10), new SqlParameter("@1", SqlDbType.VarChar, 12) };
    //cmdParams[0].Value = ddlYQXZ.SelectedValue;
    //cmdParams[1].Value = sID;
    //ci = new CommandInfo();
    //ci.CommandText = sSql;
    //ci.Parameters = cmdParams;
    //cis.Add(ci);
    //}
    #endregion
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
      SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@cbsjid", SqlDbType.Int),
        new SqlParameter("@sccbsjid", SqlDbType.Int),
        new SqlParameter("@scyqxzid", SqlDbType.Int)};
      cmdParams[0].Value = sID;
      cmdParams[1].Value = cbsjid;
      cmdParams[2].Value = ddlYQXZ.SelectedValue;
      SqlDataReader dr = DbHelperSQL.RunProcedure("p_jgtz_insert", cmdParams);
      dr.Close();
      #region
      //ProjectPrivate.CreateZHCX(sKHID, Convert.ToDateTime(dateJG.Text).ToString("yyyy-MM-dd"));
      //ProjectPrivate.UpdateCB(sKHID, Convert.ToDateTime(dateJG.Text).ToString("yyyy-MM-dd"));
      //if (string.IsNullOrEmpty(CommonUse.GetID("id")))
      //{
      //  //hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=1&id=" + sID;
      //  Server.Transfer("XSSJQLMX.aspx?mode=1&id=" + sID);
      //}
      //else
      //{
      //  string nr = "";
      //  if (txtName.Text != hlName.Value)
      //    nr += "表数由" + hlName.Value + "改为" + txtName.Text + "  ";
      //  if (dateJG.Text != hlJG.Value)
      //    nr += "日期由" + hlJG.Value + "改为" + dateJG.Text + "  ";
      //  if (ddlCBR.SelectedValue != hlCBR.Value)
      //    nr += "抄表人由" + ddlCBR.Items.FindByValue(hlCBR.Value).Text + "改为" + ddlCBR.SelectedItem.Text + "  ";
      //  if (nr != "")
      //    rzcz.InsertRZ("修改抄表数据:客户编码为" + lbKHID.Text + ",日期为" + dateJG.Text + nr);
      //  //hlQLMX.NavigateUrl = "XSSJQLMX.aspx?mode=0&id=" + sID;
      //  Server.Transfer("XSSJQLMX.aspx?mode=0&id=" + sID);
      //}
      #endregion
    }
  }

  //插入
  protected void Insert(DataSet ds)
  {
    string sID = CommonUse.GetID("sjlr_cbsj", "cbsjid");
    //if (CBSJ.ExistID(lbKHID.Text, Convert.ToDateTime(dateJG.Text).ToString("yyyyMMdd")))
    //{
    //  lbMessage.Text = "此客户于今日已录入";
    //  return;
    //}
    Save(sID, ds);
  }

  //修改
  protected void Update(DataSet ds)
  {
    List<CommandInfo> cis = new List<CommandInfo>();
    CommandInfo ci = new CommandInfo();
    CBSJ_Model model = CBSJ.GetModel(Convert.ToInt32(bccbsjid));
    model.CBSJ = Convert.ToDateTime(dateJG.Text);
    model.CBBZ = txtBZ.Text;
    ci = CBSJ.Update(model);
    cis.Add(ci);
    JGTZ_CBSJ_Model jgtz_model = JGTZ_CBSJ.GetModel(Convert.ToInt32(bccbsjid));
    jgtz_model.CBSJ = Convert.ToDateTime(dateJG.Text);
    jgtz_model.CBBZ = txtBZ.Text;
    ci = JGTZ_CBSJ.Update(jgtz_model);
    cis.Add(ci);
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
      TextBox txt = (TextBox)Panel1.FindControl("t" + ds.Tables[0].Rows[i]["yqxzid"].ToString());
      //if (txt.Text != "0")
      //{
      JGTZ_BQQL_Model jgtz_bqql_model = JGTZ_BQQL.GetModel(Convert.ToInt32(ds.Tables[0].Rows[i]["yqxzid"].ToString()),
        Convert.ToInt32(bccbsjid));
      jgtz_bqql_model.BQQL = Convert.ToInt32(txt.Text);
      ci = JGTZ_BQQL.Update(jgtz_bqql_model);
      cis.Add(ci);
      //}
    }
    if (DbHelperSQL.ExecuteSqlTran(cis) > 0)
    {
      lbMessage.Text = "保存成功";
      SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@cbsjid", SqlDbType.Int),
        new SqlParameter("@sccbsjid", SqlDbType.Int),
        new SqlParameter("@scyqxzid", SqlDbType.Int)};
      cmdParams[0].Value = bccbsjid;
      cmdParams[1].Value = cbsjid;
      cmdParams[2].Value = ddlYQXZ.SelectedValue;
      SqlDataReader dr = DbHelperSQL.RunProcedure("p_jgtz_insert", cmdParams);
      dr.Close();
    }
  }

  protected void ddlYQXZ_SelectedIndexChanged(object sender, EventArgs e)
  {
    //initJG();
    initFY();
    //InitPanel();
  }

  protected void initFY()
  {
    lbBCQL.Text = "0";
    DataSet ds = BQQL.Get_BQQL(cbsjid, ddlYQXZ.SelectedValue);
    if (ds.Tables[0].Rows.Count > 0)
    {
      lbSCJG.Text = ds.Tables[0].Rows[0]["jg"].ToString();
      lbBCQL.Text = ds.Tables[0].Rows[0]["bqql"].ToString();
    }
    //else
    //{
    //}
    //if (lbSCJG.Text == "0")
    //  txtName.Text = "0";
    //else
    //  txtName.Text = ((Convert.ToDouble(lbBCJG.Text) - Convert.ToDouble(lbSCJG.Text)) * Convert.ToDouble(lbBCQL.Text)).ToString();
  }

  protected void InitYQXZ()
  {
    //上次用气性质
    DataSet ds = KHXX.GetYQXZ_ALL(lbKHID.Text);
    DataRow dr_s = ds.Tables[0].NewRow();
    dr_s["yqxzid"] = "0";
    dr_s["yqxz"] = "无";
    ds.Tables[0].Rows.InsertAt(dr_s, 0);
    ddlYQXZ.DataValueField = "yqxzid";
    ddlYQXZ.DataTextField = "yqxz";
    ddlYQXZ.DataSource = ds;
    ddlYQXZ.DataBind();
    //if (ds.Tables[0].Rows.Count > 0)
    //ddlYQXZ.SelectedValue = ds.Tables[0].Rows[0]["yqxzid"].ToString();
    //initJG(); 
  }

  protected void initJG()
  {
    //lbSCJG.Text = "0";
    //DataTable dt = JGGL.Get_YQJG(ddlYQXZ.SelectedValue, dateJG.Text);
    //if (dt.Rows.Count > 0)
    //{
    //  lbSCJG.Text = dt.Rows[0]["jg"].ToString();
    //}
    //else if (dt.Rows.Count > 1)
    //{
    //  lbSCJG.Text = dt.Rows[1]["jg"].ToString();
    //  //lbBCJG.Text = ds.Tables[0].Rows[0]["jg"].ToString();
    //}
    //else
    //{
    //  lbSCJG.Text = "0";
    //  //lbBCJG.Text = "0";
    //}
  }
}
